package site.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import neoutil.CommonEntity;
import neoutil.CommonService;
import neoutil.DaoSupport;
import neoutil.Page;
import site.dao.ResourceDao;
import site.dao.ReviewDAO;
import site.entity.Review;
import site.entity.User;

public class ReviewRepository extends CommonService {

	
	
	/**
	 * @method main
	 * @info author:陈亚鹏  date:2012-9-21 下午04:39:26 version:1.0
	 * @param args void
	 */
	public static void main(String[] args) {
		
	}

	public void insert(Review review) {

		Connection connection = DaoSupport.getConnection();
		ReviewDAO rd = new ReviewDAO(connection);
		try {
			rd.save(review);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public Review findById(int i) throws SQLException {

		Connection connection = DaoSupport.getConnection();
		ReviewDAO rd = new ReviewDAO(connection);
		Review review = null;
		try {
			review = rd.getById((long)i);
		}finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}		
		return review;
	}

	public Page<Review> getReviewsByRid(Page<Review> page,int rid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			ReviewDAO rd = new ReviewDAO(connection);
			String sql = "SELECT * FROM review WHERE resourceid = ? AND ind=1"
					+ " ORDER BY createtime DESC";
			return rd.find(page, sql, new Object[] { rid });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public void delete(Review review) {
		Connection connection = DaoSupport.getConnection();
		ReviewDAO rd = new ReviewDAO(connection);
		try {
			rd.delete(review);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
	}

}
